<?php
 include_once('config/config.php');
 // 判断有没有表单提交过来
 if($_POST){
	// 接收全部参数
	$params = $_POST;
	
	$action = $_POST['action'];
	
	    if($action == 'check')
	    {
	      $val = $_POST['val'];
	
	      $admin = find("SELECT * FROM `pre_admin` WHERE `username` = '$val'");
	
	      if($admin)
	      {
	        error('用户名已存在');
	      }else{
	        success('用户名可以使用');
	      }
	    }
	
	// 确认密码
	if($params['password'] != $params['passwords']){
		ShowMsg('密码不一致');
		exit;
	}
	
	// 密码盐
	$salt = build_ranstr();
	if(!$salt){
		echo '密码盐为空';
		exit;
	}
	// 将密码加密码盐拼接加密
	$password = md5($params['password'].$salt);
	// 封装一个数组
	$data = [
		'username'=>$params['username'],
		'password' => $password,
		'salt' => $salt,
	];
	// 调用上传函数
	$avatar = upload('avatar');
	
	if($avatar['code'] == 0){
		ShowMsg('请选择上传的图片');
		exit;
	}else{
		$data['avatar'] = $avatar['data'];
	}
	
	$result = insert('admin',$data);
	
	if($result){
		ShowMsg('新增成功','AdminList.php');
		exit;
	}else{
		ShowMsg('新增失败');
		exit;
	}
 }
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php include_once('common/meta.php'); ?>
	</head>
	<style>
		.avatar{
			width: 200px;
			height: 200px;
			display: inline-block;
			user-select: none;
			cursor: pointer;
		}
		.avatar img{
			width: 200px;
			height: 200px;
		}
		
	</style>
	<body>

		<?php include_once('common/header.php'); ?>

		<?php include_once('common/sidebar.php'); ?>

		<div class="content">
			<div class="header">
				<h1 class="page-title">发布文章</h1>
			</div>
			<ul class="breadcrumb">
				<li><a href="index.html">Home</a> <span class="divider">/</span></li>
				<li class="active">Index</li>
			</ul>

			<div class="container-fluid">
				<div class="row-fluid">

					<div class="btn-toolbar">
						<button class="btn btn-primary" onClick="location='Adminlist.php'"><i class="icon-list"></i>管理员列表</button>
						<div class="btn-group">
						</div>
					</div>

					<div class="well">
						<div id="myTabContent" class="tab-content">
							<div class="tab-pane active in" id="home">
								<form method="post" enctype="multipart/form-data">
									<!-- 用户名 -->
									<label>用户名</label>
									<input type="text" name="username" class="input-xxlarge" placeholder="请输入用户名" required />
									<span id="username"></span>
									<!-- 密码 -->
									<label>密码</label>
									<input type="text" name="password" class="input-xxlarge" placeholder="请输入密码" required />
									<!-- 确认密码 -->
									<label>确认密码</label>
									<input type="text" name="passwords" class="input-xxlarge" placeholder="请确认密码" required />
									<!-- 头像 -->
									<label>头像</label>
									<input type="file" name="avatar" id="avatar" hidden="hidden" />
									<label for="avatar" class="avatar">
										<img src="static/images/upload.png" id="img" />
									</label>
									<br />
									<button type="button" class="btn btn-primary">选择文件</button>
									<!-- 提交 -->
									<button type="submit" class="btn btn-primary">提交</button>
								</form>
							</div>
						</div>
					</div>
					<?php include_once('common/footer.php'); ?>

				</div>
			</div>
		</div>

		<?php include_once('common/script.php'); ?>

	</body>
</html>
<script type="text/javascript">
	// 图片预览函数
	    function GetObjectURL(file) 
	    {
	      // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
	      var url = null;
	
	      //判断是否为IE浏览器还是google还是其他浏览器
	      if (window.createObjectURL != undefined) 
	      {
	        url = window.createObjectURL(file)
	      } else if (window.URL != undefined) 
	      {
	        url = window.URL.createObjectURL(file)
	      } else if (window.webkitURL != undefined) 
	      {
	        url = window.webkitURL.createObjectURL(file)
	      }
	
	      return url
	    }
		// 
		$('#avatar').change(function(){
			// 获取文件
			let file = $(this)[0].files[0] ? $(this)[0].files[0] : {};
			console.log($(this))
			// 获取文件路径
			let url = GetObjectURL(file);
			console.log(url)
			// 将文件路径添加进img
			$('#img').attr('src',url);
		})
		
		// 获取文本框的内容
		$('input[name="username"]').change(function(){
			let val = $(this).val();
			 // 发起异步请求
			    $.ajax({
			      type:'post',
			      url:'AdminAdd.php',
			      data:{
			        val,
			        action:'check'
			      },
			      dataType:'json',
			      success:function(result)
			      {
			        if(result.code === 1)
			        {
			          $('#username').html(result.msg).css({"color":"green"})
			        }else{
			          $('#username').html(result.msg).css({"color":"red"})
			        }
			      }
			    })
		})
</script>